package Server;

import Client.ClientInitClose;
import module.ExamQuestions;
import module.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ServerExam {

    private static ServerInitClose server;
    private static List<ExamQuestions> examQuestions = new ArrayList<>();
    private static List<Student> students = new ArrayList<>();

    public static ObjectInputStream objectInput;
    public static ObjectOutputStream objectOutput;
    private static int checkflag;

    //读取学生信息
    public static void readFile1(){
        try {
            objectInput = new ObjectInputStream(new FileInputStream("d:/test/listStudents.txt"));
            Object obj = objectInput.readObject();
            students = (List<Student>) obj;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != objectInput) {
                try {
                    objectInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //将信息写入文件及时更新
    public static void writeFile1(){
        try {
            objectOutput = new ObjectOutputStream(new FileOutputStream("d:/test/listStudents.txt"));
            objectOutput.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != objectOutput) {
                try {
                    objectOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //读取考题
    public static void readFile(){
        try {
            objectInput = new ObjectInputStream(new FileInputStream("d:/test/listQuestions.txt"));
            Object obj = objectInput.readObject();
            examQuestions = (List<ExamQuestions>) obj;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != objectInput) {
                try {
                    objectInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //开始考试
    public void exam() throws IOException, ClassNotFoundException {

        server.getOut().writeObject(examQuestions);
        String str = (String) server.getIn().readObject();
        String num = str.split(";")[0];
        String score = str.split(";")[1];
        if("bye".equals(num)){
            return;
        }else{
            for(Student student : students){
                if(num.equals(student.getAccount())){
                    student.setScores(score);
                    System.out.println("成绩录入成功！");
                    writeFile1();
                }
            }
        }
    }
    //考试系统
    public void serverExam(ServerInitClose s) throws IOException, ClassNotFoundException {

        server = s;
        readFile();
        while (true) {
            readFile1();
            String string = (String) server.getIn().readObject();
            String str1 = string.split(";")[0];
            //开始考试
            if ("beg".equals(str1)) {
                exam();
                //查询成绩
            } else if ("find".equals(str1)) {
                String num = string.split(";")[1];
                for(Student student : students){
                    if(num.equals(student.getAccount())){
                        System.out.println("成绩为："+student.getScores());
                        server.getOut().writeObject(student.getScores());
                    }
                }
                //导出成绩
            }else if("all".equals(str1)){
                server.getOut().writeObject(students);
                //退出系统
            }else if("end".equals(str1)){
                return;
            }
        }
    }
}
